import Vue from 'vue'
import VueRouter from 'vue-router'
import dashbaord from '../views/dashboard'
import login from '../views/login'
import register from '../views/register'
import Layout from '@/layout'
import store from '@/store/index'
Vue.use(VueRouter)

const routes = [
  {
    path: '/dashboard',
    name: 'dashboard',
    component: dashbaord
  },
  {
    path: '/',
    name: 'login',
    component: login,
    hidden:true,
  },
  {
    path: '/register',
    name: 'register',
    component: register
  },
  {
    path: '/home',
    component: Layout,
    redirect: '/shop',
    meta: {
      title: '商品后台',
      icon: 'shopping'
    },
    children: [
    {
      path: '/manage',
      name: 'manage',
      component: () => import('@/views/shop/index'),
      meta: {
        title: '商品数据',
        icon: 'dashboard'
      }
    },
    {
      path: '/type',
      name: 'type',
      component: () => import('@/views/type/index'),
      meta: {
        title: '类别数据',
        icon: 'major'
      }
    }
    ,
    {
      path: '/store',
      name: 'store',
      component: () => import('@/views/store/index'),
      meta: {
        title: '店铺数据',
        icon: 'clazz'
      }
    },
    {
      path: '/tags',
      name: 'tags',
      component: () => import('@/views/tags/index'),
      meta: {
        title: '标签数据',
        icon: 'drag'
      }
    },
    {
      path: '/goods',
      name: 'goods',
      component: () => import('@/views/shop/goods'),
      meta: {
        title: '商城详情修改',
        icon: 'topic'
      },
      hidden: true
    },
      {
        path: '/user',
        name: 'user',
        component: () => import('@/views/user/index'),
        meta: {
          title: '用户管理',
          icon: 'topic'
        },
        hidden: false
      },
    {
      path: '/creat_goods',
      name: 'creatGoods',
      component: () => import('@/views/shop/creatGoods'),
      meta: {
        title: '商城详情新增',
        icon: 'topic'
      },
      hidden: true
    },
  ]
  },

]

const router = new VueRouter({
  routes
})
router.beforeEach((to,from,next) => {
  if(['login','register'].includes(to.name))return  next()
  if(to.name === 'dashboard'){ // 进入大屏
    if (window.sessionStorage.getItem('user') === 'true'){
      next()
    }else{
      next({name:'login'})
    }
  }else{ // 进入后台或者其他
    if(window.sessionStorage.getItem('manager') === 'true') {
      next()
    }else{
      next({name:'login'})
    }
  }
})

export default router
